package cn.yesway.dao;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import cn.yesway.entity.AcctUser;

@Repository
public interface AcctUserDao extends CrudRepository<AcctUser, Long> {
	public abstract AcctUser findByLoginName(String loginName);

	// Enabling ignoring case for an individual property
	// AllIgnoreCase upper(acctuser0_.login_name)=upper(?)
	public abstract AcctUser findByLoginNameIgnoreCase(String loginName);

	// And --- 等价于 SQL 中的 and 关键字
	public abstract AcctUser findByLoginNameAndPassword(String loginName, String password);

	// Or --- 等价于 SQL 中的 or 关键字
	public abstract List<AcctUser> findByLoginNameOrEmail(String loginName, String email);

	// 排序
	// Enabling static ORDER BY for a query Asc Desc
	public abstract List<AcctUser> findByEmailOrderByLoginNameAsc(String email);

	// 排序的另外一个方法
	public abstract List<AcctUser> findByEmail(String email, Sort sort);

	// Limiting the result size of a query with Top and First
	// 获取第一条记录 findFirst findTop findFirst10 findTop10
	public abstract AcctUser findFirstByEmailOrderByLoginNameAsc(String email);

	// Enables the distinct flag for the query
	// 没理解，还有问题
	public abstract List<AcctUser> findDistinctByEmail(String email);

	// 分页
	public abstract Page<AcctUser> findByEmail(String email, Pageable pageable);

	// 查询所有
	public abstract Page<AcctUser> findAll(Pageable pageable);
	public abstract List<AcctUser> findAll(Sort sort);

	// count(acctuser0_.id)
	public abstract Long countByEmail(String email);

	@Modifying
	@Query("UPDATE AcctUser u SET u.loginTime= :loginTime WHERE u.id = :acctUserId")
	public abstract int updateLoginTime(@Param("acctUserId") long acctUserId, @Param("loginTime") Date loginTime);

	@Modifying
	@Query("DELETE FROM AcctUser u WHERE u.id IN ?1")
	public abstract int deleteByIds(List<Long> ids);

	// 通过观察sql语句发现，先查询列表，之后使用多个delete语句进行删除？
	public abstract Long deleteByEmail(String email);

	// 删除记录，同时返回删除的记录列表
	public abstract List<AcctUser> removeByEmail(String email);
}
